var fun_aes = require('aeswh.js') //引用AES源码js
var key = fun_aes.CryptoJS.enc.Utf8.parse("wanhong2017abcWH")//十六位十六进制数作为秘钥
 var iv = fun_aes.CryptoJS.enc.Utf8.parse('') //十六位十六进制数作为秘钥偏移量

function Encrypt(word) {
  var srcs = fun_aes.CryptoJS.enc.Utf8.parse(word);
  var encrypted = fun_aes.CryptoJS.AES.encrypt(srcs, key, {
    iv: iv,
    mode: fun_aes.CryptoJS.mode.ECB,
    padding: fun_aes.CryptoJS.pad.Pkcs7
  });
  //返回大写十六进制加密结果
  return encrypted.ciphertext.toString().toUpperCase();
 
}

function EncryptBASE64(word) {
  var srcs1 = fun_aes.CryptoJS.enc.Utf8.parse(word);
  var encrypted1 = fun_aes.CryptoJS.AES.encrypt(srcs1, key, {
    iv: iv,
    mode: fun_aes.CryptoJS.mode.ECB,
    padding: fun_aes.CryptoJS.pad.Pkcs7
  });
  //返回base64加密结果
  return encrypted1.toString();
}

function Decrypt(word) {
  var encryptedHexStr = fun_aes.CryptoJS.enc.Hex.parse(word);
  var srcs = fun_aes.CryptoJS.enc.Base64.stringify(encryptedHexStr);
  var decrypt = fun_aes.CryptoJS.AES.decrypt(srcs, key, {
    iv: iv,
    mode: fun_aes.CryptoJS.mode.ECB,
    padding: fun_aes.CryptoJS.pad.Pkcs7
  });
  var decryptedStr = decrypt.toString(fun_aes.CryptoJS.enc.Utf8);
  return decryptedStr.toString();
  //return JSON.parse(decryptedStr.toString());
}



//暴露接口
module.exports.Decrypt = Decrypt;
module.exports.Encrypt = Encrypt;
module.exports.EncryptBASE64 = EncryptBASE64;